package com.tourism.controller;


import com.tourism.core.common.utils.Result;
import com.tourism.domain.dto.attraction.AttractionQueryDTO;
import com.tourism.domain.entity.Attraction;
import com.tourism.service.AttractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 景点控制器
 *
 * @author lucky
 */
@RequestMapping("/api/admin/attraction")
@RestController
public class AttractionController {

    @Autowired
    private AttractionService attractionService;

    @PostMapping("page")
    @PreAuthorize("hasAuthority('attraction:page')")
    public Result<?> getPage(@RequestParam("pageIndex") Integer pageIndex,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestBody AttractionQueryDTO attractionQueryDTO) {
        return Result.success(attractionService.getPage(pageIndex, pageSize, attractionQueryDTO));
    }


    @GetMapping("all")
    public Result<?> view(@ModelAttribute AttractionQueryDTO attractionQueryDTO) {
        return Result.success(attractionService.getAllAttraction(attractionQueryDTO));
    }

    @PostMapping("save")
    @PreAuthorize("hasAuthority('attraction:save')")
    public Result<Boolean> save(@RequestBody Attraction attraction) {
        return attractionService.save(attraction) ? Result.success() : Result.failure();
    }

    @PutMapping("update")
    @PreAuthorize("hasAuthority('attraction:update')")
    public Result<Boolean> update(@RequestBody Attraction attraction) {
        return attractionService.updateById(attraction) ? Result.success() : Result.failure();
    }


    @DeleteMapping("delete")
    @PreAuthorize("hasAuthority('attraction:delete')")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        return attractionService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }

    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('attraction:get')")
    public Result<?> get(@PathVariable Long id) {
        return Result.success(attractionService.getAttractionById(id));
    }

}
